package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.service.admin.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {
    private final CategoryMapper categoryMapper;

    @Override
    public void save(Category categoryDTO) {
        log.info("新增分类：{}",categoryDTO);
        //以下字段已经统一赋值
        //更新创表时间时间
        categoryDTO.setCreateTime(LocalDateTime.now());
        categoryDTO.setUpdateTime(LocalDateTime.now());
        //修改人
        categoryDTO.setUpdateUser(BaseContext.getCurrentId());
        //设置默认状态为1
        categoryDTO.setStatus(StatusConstant.DISABLE);
        //创建人
        categoryDTO.setUpdateUser(BaseContext.getCurrentId());
       categoryMapper.save(categoryDTO);
    }

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        //设置分页,设置分页参数
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //调用mapper层实现分页查询(使用PageHelper插件调用mapper查询结果的接受类型统一为page<>型)
        Page<Category> pageResults=categoryMapper.page(categoryPageQueryDTO);
        return new PageResult(pageResults.getTotal(), pageResults.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public List<Category>list(Integer type) {
        return categoryMapper.list(type);
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {

        Category category = new Category();
        // 将 categoryDTO 的属性复制到 employee 中
        BeanUtils.copyProperties(categoryDTO, category);
        //以下字段已经统一赋值
        //更新时间
        category.setUpdateTime(LocalDateTime.now());
        //更新创新人
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.updateCategory(category);
    }

    @Override
    public void ActivateAndForbiddenEmp(String status, Integer id) {
        categoryMapper.ActivateAndForbiddenEmp(status,id);
    }

}
